package buffbuildergui;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class OutPort extends JPanel implements MouseListener{
    int max;
    EntityPanel owner;
    public static final int eIA = 0;
    public static final int ePA = 1;
    public static final int eOA = 2;
    public static final int eEM = 3;
    public static final int eBM = 4;
    public static final int eFC = 5;
    int type;
    public OutPort(int m,int t, EntityPanel ep) {
        super();
        max = m;
        owner = ep;
        type = t;
        setBackground(Color.black);
        setBorder(new LineBorder(Color.black));
        addMouseListener(this);
    }

    @Override
    public void mouseClicked(MouseEvent mouseEvent) {
        int count = mouseEvent.getClickCount();
        if(count == 2) {
            Entity e = null;
            EntityPanel ep = null;
            if(type == eIA)
            {
                e = new InstantAction();
                ep = new InstantActionPanel("Instant Action",e,owner);
                if(owner instanceof BuffPanel)
                {
                    owner.attachedEntity.instant++;
                    int i = owner.convertRowToID("numInstantActions");
                    Property target = owner.virtualList.get(i);
                    String str = owner.attachedEntity.instant + "";
                    target.overwrite(str);
                    owner.propertyData.set(i,target.stringify());
                }
            }
            else if(type == ePA) {
                e = new PeriodicAction();
                ep = new PeriodicActionPanel("Periodic Action",e,owner);
                if(owner instanceof BuffPanel)
                {
                    owner.attachedEntity.periodic++;
                    int i = owner.convertRowToID("numPeriodicActions");
                    Property target = owner.virtualList.get(i);
                    String str = owner.attachedEntity.periodic + "";
                    target.overwrite(str);
                    owner.propertyData.set(i,target.stringify());
                }
            }
            else if(type == eOA) {
                e = new OverTimeAction();
                ep = new OverTimeActionPanel("Over Time Action",e,owner);
                if(owner instanceof BuffPanel)
                {
                    owner.attachedEntity.over++;
                    int i = owner.convertRowToID("numOverTimeActions");
                    Property target = owner.virtualList.get(i);
                    String str = owner.attachedEntity.over + "";
                    target.overwrite(str);
                    owner.propertyData.set(i,target.stringify());
                }
            }
            else if(type == eEM) {
                e = new EntityModifier();
                ep = new EntityModifierPanel("Entity Modifier",e,owner);
                if(owner instanceof BuffPanel)
                {
                    owner.attachedEntity.modifiers++;
                    int i = owner.convertRowToID("numEntityModifiers");
                    Property target = owner.virtualList.get(i);
                    String str = owner.attachedEntity.modifiers + "";
                    target.overwrite(str);
                    owner.propertyData.set(i,target.stringify());
                }
            }
            else if(type == eBM) {
                e = new EntityBoolModifier();
                ep = new EntityBoolModifierPanel("Entity Bool Modifier",e,owner);
                if(owner instanceof BuffPanel)
                {
                    owner.attachedEntity.bools++;
                    int i = owner.convertRowToID("numEntityBoolModifiers");
                    Property target = owner.virtualList.get(i);
                    String str = owner.attachedEntity.bools + "";
                    target.overwrite(str);
                    owner.propertyData.set(i,target.stringify());
                }
            }
            else if(type == eFC) {
                e = new FinishCondition();
                ep = new FinishConditionPanel("Finish Condition",e,owner);
                if(owner instanceof BuffPanel)
                {
                    owner.attachedEntity.finishers++;
                    int i = owner.convertRowToID("numFinishConditions");
                    Property target = owner.virtualList.get(i);
                    String str = owner.attachedEntity.finishers + "";
                    target.overwrite(str);
                    owner.propertyData.set(i,target.stringify());
                }
            }
            else {
                return;
            }
            owner.getMainFrame().bypass.add(ep);
            owner.children.add(ep);
            owner.getMainFrame().cm.registerComponent(ep);
            ep.setLocation(owner.getLocation().x+250,owner.getLocation().y+100);
            ep.frontify();
        }
    }

    @Override
    public void mousePressed(MouseEvent mouseEvent) {
        // TODO Implement this method
    }

    @Override
    public void mouseReleased(MouseEvent mouseEvent) {
        // TODO Implement this method
    }

    @Override
    public void mouseEntered(MouseEvent mouseEvent) {
        // TODO Implement this method
    }

    @Override
    public void mouseExited(MouseEvent mouseEvent) {
        // TODO Implement this method
    }
}
